import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:rxdart/rxdart.dart';
import 'package:schoollifedemo/base/common_function.dart';
import 'package:schoollifedemo/base/routers.dart';
import 'package:schoollifedemo/base/url_contract.dart';
import 'package:schoollifedemo/commonwidget/common_base_viewmodel.dart';
import 'package:schoollifedemo/entity/Entity.dart';

class HomeSchoolPageViewModel extends BaseViewModel {
  UserMemorandumListResponse? _userMemorandumResponse;

  UserMemorandumListResponse? get userMemorandumResp => _userMemorandumResponse;

  SchoolAnnouncementResponse? _schoolAnnouncementResponse;

  SchoolAnnouncementResponse? get schoolAnnouncementResp =>
      _schoolAnnouncementResponse;

  BehaviorSubject sourceLoadingState = BehaviorSubject<CommonLoadingState?>();

  BehaviorSubject memorandumLoadingState =
      BehaviorSubject<CommonLoadingState?>();
  BehaviorSubject schoolNoticeLoadingState =
      BehaviorSubject<CommonLoadingState?>();

  // 跳转到编辑备忘录页面
  Future<void> turnToEditMemorandumPage(
    BuildContext context, {
    String? noteId,
    bool? isEditMode,
  }) async {
    final params = <String, Object?>{};
    if (noteId?.isNotEmpty == true) {
      params['note_id'] = noteId;
    }
    switch (isEditMode) {
      case true:
        params['is_edit_mode'] = '1';
        break;
      default:
        params['is_edit_mode'] = '0';
        break;
    }
    await getApplication().routerNavigateTo(
      context,
      RoutersPath.editMemorandumPage,
      params: params,
    );
  }

  // 跳转到所有备忘录页面
  Future<void> turnToAllMemorandumPage(BuildContext context) async {
    return await getApplication()
        .routerNavigateTo(context, RoutersPath.selectAllMemorandumPage);
  }

  // 跳转到单个备忘录页面
  Future<void> turnToSingleMemorandumPage(
    BuildContext context, {
    required String noteId,
  }) async {
    await turnToEditMemorandumPage(context, noteId: noteId, isEditMode: false);
  }

  // 跳转到单个学校公告页面
  Future<void> turnToSingleAnnouncementPage(
    BuildContext context, {
    required String mainTitle,
  }) async {
    await turnToMultiAnnouncementPage(context, mainTitle: mainTitle);
  }

  // 跳转到多个学校公告页面
  Future<void> turnToMultiAnnouncementPage(
    BuildContext context, {
    String? mainTitle,
  }) async {
    final params = <String, Object>{};
    if (mainTitle?.isNotEmpty == true) {
      params['main_title_md5'] = (await getChannelService()
              .getCommonChannelFunc()
              ?.convertMD5(mainTitle ?? '')) ??
          '';
    }
    return await getApplication().routerNavigateTo(
      context,
      RoutersPath.seeMoreAnnouncementPage,
      params: params,
    );
  }

  // 拉取学校信息
  Future<void> pullSchoolAnnouncement({
    OnSuccess? onSuccess,
    OnFailure? onFailure,
  }) async {
    final params = <String, Object?>{};
    params['account'] = getApplication().getLoginAccount() ?? '';
    final result = await getNetworkUtil()
        .postHttp(UrlContract.pullUserSchoolAnnouncement, params: params);
    final resp = SchoolAnnouncementResponse.fromMap(result);
    if (resp.status == 0) {
      _schoolAnnouncementResponse = resp;
      schoolNoticeLoadingState.add(CommonLoadingState.successTypeOne);
      if (onSuccess != null) {
        onSuccess();
      }
    } else {
      schoolNoticeLoadingState.add(CommonLoadingState.failure);
      if (onFailure != null) {
        onFailure(resp.msg);
      }
    }
  }

  // 拉取备忘录信息
  Future<void> pullUserMemorandumList({
    int pageSize = 5,
    int currentPage = 1,
    bool refreshQuickly = false,
    bool append = false,
    OnSuccess? onSuccess,
    OnFailure? onFailure,
  }) async {
    if (_userMemorandumResponse?.haveMore == false && !refreshQuickly) {
      return;
    }
    final params = <String, Object>{};
    params['account'] = getApplication().getLoginAccount() ?? '';
    params['page_size'] = pageSize;
    if (append) {
      params['current_page'] = (_userMemorandumResponse?.currentPage ?? 0) + 1;
    } else {
      params['current_page'] = currentPage;
    }
    final result = await getNetworkUtil()
        .postHttp(UrlContract.pullUserMemorandumList, params: params);
    final resp = UserMemorandumListResponse.fromMap(result);
    if (resp.status == 0) {
      if (append) {
        _userMemorandumResponse?.currentPage = resp.currentPage;
        _userMemorandumResponse?.haveMore = resp.haveMore;
        _userMemorandumResponse?.pageSize = resp.pageSize;
        resp.memorandumList?.forEach((element) {
          _userMemorandumResponse?.memorandumList?.add(element);
        });
      } else {
        _userMemorandumResponse = resp;
      }
      memorandumLoadingState.add(CommonLoadingState.successTypeOne);
      if (onSuccess != null) {
        onSuccess();
      }
    } else {
      memorandumLoadingState.add(CommonLoadingState.failure);
      if (onFailure != null) {
        onFailure(resp.msg);
      }
    }
  }

  // 拉取课程信息
  Future<void> pullStudentSource() async {
    sourceLoadingState.add(CommonLoadingState.successTypeOne);
  }
}
